package com.csw.mario;

import java.awt.image.BufferedImage;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2022/11/8 22:27
 */
public class Mario implements Runnable {
    //马里奥横纵坐标
    private int x;
    private int y;
    //x移动速度,y跳跃速度
    private int xSpeed;
    private int ySpeed;
    //表示马里奥上升的时间(上升到最高点时upTime=0)
    private int upTime = 0;
    //判断马里奥是否走到了城堡门口
    private boolean isOK;

    //表示当前状态
    private String status;
    //用于显示当前状态的图像
    private BufferedImage show = null;
    //用于获取障碍物的信息
    private BackGround backGround = new BackGround();
    //线程对象实现马里奥的动作
    private Thread thread = null;
    //定义一个索引
    private int index;
    //判定马里奥是否死亡
    private boolean isDeath = false;
    //马里奥积分
    private int score = 0;

    public Mario() {

    }

    public Mario(int x, int y) {
        this.x = x;
        this.y = y;
        //初始向右站立
        this.show = StaticValue.stand_R;
        //状态
        this.status = "stand-right";

        thread = new Thread(this);
        //启动线程
        thread.start();

    }

    //马里奥向左移动
    public void leftMove() {
        //改变速度
        xSpeed = -5;
        //判断马里奥是否碰到了旗子,碰到了不再移动
        if (backGround.isReach()) {
            xSpeed = 0;
        }
        //判断马里奥是否处于空中
        if (status.indexOf("jump") != -1) {
            status = "jump--left";
        } else {
            status = "move--left";
        }

    }

    //马里奥向右移动
    public void rightMove() {
        //改变速度
        xSpeed = 5;
        //判断马里奥是否碰到了旗子,碰到了不再移动
        if (backGround.isReach()) {
            xSpeed = 0;
        }
        //判断马里奥是否处于空中
        if (status.indexOf("jump") != -1) {
            status = "jump--right";
        } else {
            status = "move--right";
        }

    }

    //马里奥向左停止
    public void leftStop() {
        xSpeed = 0;
        //判断马里奥是否处于空中
        if (status.indexOf("jump") != -1) {
            status = "jump--left";
        } else {
            status = "stop--left";
        }
    }

    //马里奥向右停止
    public void rightStop() {
        xSpeed = 0;
        //判断马里奥是否处于空中
        if (status.indexOf("jump") != -1) {
            status = "jump--right";
        } else {
            status = "stop--right";
        }
    }

    public boolean isOK() {
        return isOK;
    }

    //马里奥跳跃
    public void jump() {
        //等于-1表示没有jump,不是跳跃状态才进行跳跃
        if (status.indexOf("jump") == -1) {
            //不等于-1表示有left向左
            if (status.indexOf("left") != -1) {
                status = "jump--left";
            } else {
                status = "jump--right";
            }
            ySpeed = -10;
            upTime = 7;
        }
        //判断马里奥是否碰到了旗子,碰到了不再移动
        if (backGround.isReach()) {
            ySpeed = 0;
        }
    }

    //马里奥下坠
    public void fall() {
        //向左
        if (status.indexOf("left") != -1) {
            status = "jump--left";
        } else {
            status = "jump--right";
        }
        ySpeed = 10;
    }

    //马里奥死亡
    public void death() {
        isDeath = true;
    }


    @Override
    public void run() {
        while (true) {
            //判断是否在障碍物上
            boolean onObstacle = false;
            //判断是否可以往右走
            boolean canRight = true;
            //判断是否可以往左走
            boolean canLight = true;
            //判断是否到达旗杆
            if (backGround.isFlag() && this.x >= 500) {
                this.backGround.setReach(true);
                //判断旗子下落是否完成
                if (this.backGround.isBase()) {
                    status = "move--right";
                    //判断是否移动到城堡中间
                    if (x < 690) {
                        //rightMove();
                        x += 5;
                    } else {
                        //移动到了城堡门口
                        isOK = true;
                    }
                } else {
                    //旗子在空中
                    if (y < 395) {
                        xSpeed = 0;
                        this.y += 5;
                        status = "jump--right";
                    }
                    //马里奥落到地上不应该下落
                    if (y > 395) {
                        this.y = 395;
                        status = "stop--right";
                    }

                }
            } else {
                //遍历当前所有的障碍物
                for (int i = 0; i < backGround.getObstacleList().size(); i++) {
                    Obstacle ob = backGround.getObstacleList().get(i);
                    //判断马里奥是否在障碍物上
                    if (ob.getY() == this.getY() + 25 && ob.getX() > this.x - 30 && ob.getX() < this.getX() + 25) {
                        onObstacle = true;
                    }
                    //判断马里奥是否顶到了障碍物
                    if ((ob.getY() >= this.y - 30 && ob.getY() <= this.getY() - 20) && (ob.getX() > this.x - 30 && ob.getX() < this.x + 25)) {
                        //判断是否是可消除的砖块
                        if (ob.getType() == 0) {
                            backGround.getObstacleList().remove(ob);
                            score += 1;
                        }
                        upTime = 0;
                    }
                    //判断是否可以往右走
                    if (ob.getX() == this.x + 25 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
                        canRight = false;
                    }
                    //判断左边是否有障碍物
                    if (ob.getX() == this.x - 30 && (ob.getY() > this.y - 30 && ob.getY() < this.y + 25)) {
                        canLight = false;
                    }

                }
                //判断马里奥是否碰到敌人或者踩死敌人
                for (int i = 0; i < backGround.getEnemyList().size(); i++) {
                    Enemy enemy = backGround.getEnemyList().get(i);
                    if (enemy.getY() == this.y + 20 && (enemy.getX() - 25 < this.x && enemy.getX() + 35 >= this.x)) {
                        //判定踩到蘑菇敌人
                        if (enemy.getType() == 1) {
                            enemy.death();
                            upTime = 3;
                            //-10表示向上的速度
                            ySpeed = -10;
                            score += 2;
                        } else if (enemy.getType() == 2) {
                            //碰到食人花马里奥死亡
                            death();
                        }

                    }
                    //判定碰到敌人
                    if ((enemy.getX() + 35 > this.x && enemy.getX() - 25 < this.x) && (enemy.getY() + 35 > this.y && enemy.getY() - 20 < this.y)) {
                        //马里奥死亡
                        death();
                    }
                }

                //进行马里奥跳跃的操作
                if (onObstacle && upTime == 0) {
                    if (status.indexOf("left") != -1) {
                        if (xSpeed != 0) {
                            status = "move--left";

                        } else {
                            status = "stop--left";
                        }

                    } else {
                        if (xSpeed != 0) {
                            status = "move--right";

                        } else {
                            status = "stop--right";
                        }
                    }
                } else {
                    if (upTime != 0) {
                        upTime--;
                    } else {
                        fall();
                    }
                    y += ySpeed;
                }


                if (canLight && xSpeed < 0 || canRight && xSpeed > 0) {
                    x += xSpeed;
                    //判断马里奥是否到了最左边
                    if (x < 0) {
                        x = 0;

                    }
                }

                //判断马里奥是否是移动状态,来回切换1和0表示移动
                if (status.contains("move")) {
                    index = index == 0 ? 1 : 0;
                }
                //判断是否向左移动
                if ("move--left".equals(status)) {
                    show = StaticValue.run_L.get(index);
                }
                //判断是否向右移动
                if ("move--right".equals(status)) {
                    show = StaticValue.run_R.get(index);
                }
                //判断是否向左停止
                if ("stop--left".equals(status)) {
                    show = StaticValue.stand_L;
                }
                //判断是否向右停止
                if ("stop--right".equals(status)) {
                    show = StaticValue.stand_R;
                }
                //判断是否向左跳跃
                if ("jump--left".equals(status)) {
                    show = StaticValue.jump_L;
                }
                //判断是否向右跳跃
                if ("jump--right".equals(status)) {
                    show = StaticValue.jump_R;
                }

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }


    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public BufferedImage getShow() {
        return show;
    }

    public void setShow(BufferedImage show) {
        this.show = show;
    }

    public BackGround getBackGround() {
        return backGround;
    }

    public void setBackGround(BackGround backGround) {
        this.backGround = backGround;
    }

    public boolean isDeath() {
        return isDeath;
    }

    public void setDeath(boolean death) {
        isDeath = death;
    }
}
